package com.sharemarking.application.common.utils;

import com.google.gson.Gson;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;

/**
 * @author song.lian
 *
 */
public class HttpUtil {
	/**
	 * @param url
	 *            请求地址
	 * @return 请求结果
	 */
	public static String get(String url) {
		CloseableHttpClient chc = null;
		CloseableHttpResponse chr = null;
		String result = null;
		try {
			chc = HttpClients.createDefault();
			HttpGet hg = new HttpGet(url);
			chr = chc.execute(hg);
			HttpEntity hc = chr.getEntity();
			result = EntityUtils.toString(hc, "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeAll(chr, chc);
		}
		return result;
	}

	/**
	 * @param url
	 *            请求地址
	 * @param jsonObject
	 *            json数据 采用gson解析
	 * @return 返回字符串,采用utf-8编码
	 */
	public static String postJSON(String url, Object jsonObject) {
		String json = (new Gson()).toJson(jsonObject);
		return post(url, json, ContentType.APPLICATION_JSON);
	}

	public static String postXML(String url, Object xmlObject) {
		String xmlText = "";
		ByteArrayOutputStream baos = null;
		try {
			JAXBContext jaxb = JAXBContext.newInstance(xmlObject.getClass());
			baos = new ByteArrayOutputStream();
			jaxb.createMarshaller().marshal(xmlObject, baos);
			xmlText = baos.toString();
		} catch (JAXBException e) {
			e.printStackTrace();
		} finally {
			if (baos != null)
				try {
					baos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		return post(url, xmlText, ContentType.APPLICATION_XML);
	}

	private static String post(String url, String requestBody, ContentType ct) {
		CloseableHttpClient chc = null;
		CloseableHttpResponse chr = null;
		String result = null;
		try {
			chc = HttpClients.createDefault();
			HttpPost hp = new HttpPost(url);
			StringEntity se = new StringEntity(requestBody, ct);
			hp.setEntity(se);
			chr = chc.execute(hp);
			HttpEntity he = chr.getEntity();
			result = EntityUtils.toString(he, "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeAll(chr, chc);
		}
		return result;
	}

	/**
	 * 按数组顺序关闭
	 * */
	private static void closeAll(Closeable... cs) {
		for (Closeable c : cs) {
			try {
				if (c != null)
					c.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/*
	 * public static void main(String[] arg) throws JAXBException{ String
	 * url="http://211.147.244.114:9801/CASServer/SmsAPI/SendMessage.jsp";
	 * String
	 * text="userid=62872&password=hsy62872&destnumbers=15521267206&msg=测试";
	 * //String res=postXML("http://localhost:8080/wx/message/main", wx);
	 * System.
	 * out.println(post(url,text,ContentType.APPLICATION_FORM_URLENCODED)); }
	 */
}
